package com.zq.leave.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zq.leave.entity.Vacate;
import com.zq.leave.entity.dto.Result;
import com.zq.leave.service.VacateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baicun
 * @since 2020-06-20
 */
@RestController
@CrossOrigin
@RequestMapping("/vacate")

public class VacateController {

    @Autowired
    private VacateService vacateService;

    //用户请假申请，请假表单数据插入数据库
    @PostMapping("/userVacate")
    public Result userVacate(@Validated @RequestBody Vacate vacate){
        vacate.setStatus("待审批");
        vacate.setCreated(LocalDateTime.now());
        vacateService.saveOrUpdate(vacate);
        return Result.success(null);
    }

    //获取approving approved数据
    @PostMapping("/getVacateByStatus")
    public Result getVacateByStatus(@RequestBody JSONObject jsonObject){
        List<Vacate> vacateList = null;
        String username = jsonObject.getStr("username");
        String type = jsonObject.getStr("type");
        if (type.equals("approving")){
            vacateList = vacateService.list(new QueryWrapper<Vacate>().
                    eq("username",username).
                    eq("status","待审批").
                    orderByDesc("created"));

        }else{
            vacateList = vacateService.list(new QueryWrapper<Vacate>().
                    eq("username",username).
                    eq("status","待确认").
                    or().eq("username",username).
                    eq("status","不通过").
                    orderByDesc("created"));
        }
        return Result.success(vacateList);
    }

    //请假清单分页
    @PostMapping("/getVacatesByPage")
    public Result getVacatesByPage(@RequestBody JSONObject jsonObject){
        Boolean isUser = jsonObject.getBool("isUser");
        String username = jsonObject.getStr("username");
        int currentPage = jsonObject.getInt("currentPage");
        //默认5页
        Page page = new Page(currentPage,5);
        IPage iPage = null;
        if (isUser){
            iPage = vacateService.page(page,new QueryWrapper<Vacate>().
                    eq("username",username).
                    eq("status","请假成功").or().
                    eq("username",username).
                    eq("status","请假失败").orderByDesc("confirmed"));
        } else{
            iPage = vacateService.page(page,new QueryWrapper<Vacate>().
                    eq("approver",username).
                    eq("status","请假成功").or().
                    eq("approver",username).
                    eq("status","请假失败").orderByDesc("confirmed"));
        }
        return Result.success(iPage);
    }

    //获取approver请假审批数据
    @PostMapping("/getVacateApproved")
    public Result getVacateApproved(@RequestBody JSONObject jsonObject){
        String approver = jsonObject.getStr("username");
        List<Vacate> vacateList = vacateService.list(new QueryWrapper<Vacate>().
                eq("approver",approver).
                eq("status","待审批").orderByDesc("created"));
        return Result.success(vacateList);
    }

    //approver审批人审批，更新Vacated的Status,Approved
    @PostMapping("/approved")
    public Result approved(@RequestBody JSONObject jsonObject){
        Vacate vacate = vacateService.getById(jsonObject.getInt("id"));
        vacate.setBackinfo(jsonObject.getStr("backinfo"));
        vacate.setStatus(jsonObject.getStr("status"));
        vacate.setApproved(LocalDateTime.now());
        vacateService.saveOrUpdate(vacate);
        return Result.success(null);
    }

    //User请假人确认审批结果,更新Vacated的Status,Confirmed
    @PostMapping("/userConfirm")
    public Result userConfirm(@RequestBody JSONObject jsonObject){
        Vacate vacate = vacateService.getById(jsonObject.getInt("id"));
        vacate.setStatus(jsonObject.getStr("status"));
        vacate.setConfirmed(LocalDateTime.now());
        vacateService.saveOrUpdate(vacate);
        return Result.success(null);
    }

}
